home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4561 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. Path: news.nstn.ca!news
  2. From: keichele@ac.dal.ca (Klaus Eichele)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help with float/double data types
  5. Date: Wed, 31 Jan 1996 04:59:42 GMT
  6. Organization: Dalhousie University
  7. Message-ID: <4emev9$pq0@news.nstn.ca>
  8. References: <96030.105940RWL380B@MAINE.MAINE.EDU>
  9. NNTP-Posting-Host: rewasylishen.chem.dal.ca
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. <RWL380B@MAINE.MAINE.EDU> wrote:
  13.  
  14. >C++ Experts,
  15.  
  16. >I have a "simple" question re: floating point data types in C/C++.
  17. >I've traditionally programmed in Fortran but am trying to learn
  18. >new tricks....  I ran into this problem while trying to write a
  19. >filter that converts the output of one commercial program to the
  20. >input format for Arc/Info.  A simplified version (just I/O) is
  21.  
  22.    <SNIP>
  23.  
  24. >If I declare X, Y, Z and Zstd as float:
  25. >    float X=0.0,Y=0.0,Z=0.0,Zstd=0.0;
  26. >Then the output looks correct but the digits after the decimal points
  27. >in X are incorrect (original first line X = -2082085.85000).  The
  28. >first value of Y is incorrect, but the next values are correct!
  29.  
  30. > X-Coordinate     Y-Coordinate       Z-Est     Z StdDev    n
  31. >-2082085.87500    -2127847.00000    23.12121    1.77931    8
  32.  
  33.       <SNIP>
  34.  
  35. >If I declare X,Y,Z and Zstd to be double then the output is
  36. >correct.
  37.  
  38. > X-Coordinate     Y-Coordinate      Z-Est      Z StdDev    n
  39. >-2082085.85000    -2127847.12500    23.12121    1.77931    8
  40.  
  41.       <SNIP>
  42.  
  43. Hi,
  44. to give you a hint about what is going on let me first quote the
  45. comp.lang.c FAQ:
  46.  
  47.     14.1: When I set a float variable to, say, 3.1, why is printf()
  48.           printing it as 3.0999999?
  49.  
  50.     Most computers use base 2 for floating-point numbers as well as
  51.     for integers.  In base 2, 1/1010 (that is, 1/10 decimal) is an
  52.     infinitely-repeating fraction: its binary representation is 
  53.     0.0001100110011... .  
  54.     Depending on how carefully your compiler's binary/decimal
  55.     conversion routines (such as those used by printf) have
  56.     been written, you may see discrepancies when numbers (especially
  57.     low-precision floats) not exactly representable in base 2 are
  58.     assigned or read in and then printed (i.e. converted from base 10
  59.     to base 2 and back again).
  60.  
  61. With floats (usually a 4 byte entity), you will have about 7
  62. significant digits, and this is where you are running into trouble
  63. with your output of large numbers.  With doubles (in FORTRAN REAL*8),
  64. you will have approx. 15 significant digits represented properly,
  65. therefore the output seems to work in this case.
  66.  
  67. Hope this helps,
  68. Klaus
  69. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  70. Klaus Eichele         keichele@ac.dal.ca  
  71. http://ac.dal.ca/~keichele/keichele.html
  72.  
  73.